<%

Function ordenar(vetor, auxiliar, minimo, maximo, pos)

		Call Indice_start(auxiliar, minimo, maximo-1)

		Call QuickSort(vetor, auxiliar, minimo, maximo-1, pos)

End Function

Sub QuickSort(vec, ind, loBound, hiBound, pos)
  Dim pivot,loSwap,hiSwap,temp
 
  '== Two items to sort
  if hiBound - loBound = 1 then
    if vec(ind(loBound), pos) > vec(ind(hiBound), pos) then
      temp=ind(loBound)
      ind(loBound) = ind(hiBound)
      ind(hiBound) = temp
    End If
  End If
 
  '== Three or more items to sort
  pivot = ind(int((loBound + hiBound) / 2))
  ind(int((loBound + hiBound) / 2)) = ind(loBound)
  ind(loBound) = pivot
  loSwap = loBound + 1
  hiSwap = hiBound

  do
    '== Find the right loSwap
    while loSwap < hiSwap and vec(ind(loSwap), pos) <= vec(pivot, pos)
      loSwap = loSwap + 1
    wend
    '== Find the right hiSwap
    while vec(ind(hiSwap), pos) > vec(pivot, pos)
      hiSwap = hiSwap - 1
    wend
    '== Swap values if loSwap is less then hiSwap
    if loSwap < hiSwap then
      temp = ind(loSwap)
      ind(loSwap) = ind(hiSwap)
      ind(hiSwap) = temp
    End If
  loop while loSwap < hiSwap
 
  ind(loBound) = ind(hiSwap)
  ind(hiSwap) = pivot
 
  '== Recursively call function .. the beauty of Quicksort
    '== 2 or more items in first section
    if loBound < (hiSwap - 1) then Call QuickSort(vec,ind,loBound,hiSwap-1,pos)
    '== 2 or more items in second section
    if hiSwap + 1 < hibound then Call QuickSort(vec,ind,hiSwap+1,hiBound,pos)
 
End Sub  'QuickSort

Sub Indice_start(auxiliar, minimo, maximo)

		DIM i
		
		For i=minimo to maximo
		
			auxiliar(i)=i
		
		Next


End Sub  

%>